//
//  OilDropManageViewController.m
//  阿帕
//
//  Created by mac on 2020/5/26.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "OilDropRecordViewController.h"
#import "OilAddRecordCell.h"

@interface OilDropRecordViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView      *tableView;
@property (nonatomic, strong) NSMutableArray   *dataArray;
@property (nonatomic, assign) NSInteger         pageNum;
@property (strong , nonatomic)UIImageView      *foundImageView;

@end

@implementation OilDropRecordViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    _pageNum = 1;

    self.view.backgroundColor = BackgroundColor;
    
    self.navigationItem.title = @"油卡变动记录";
    
    self.tableView.backgroundColor = BackgroundColor;

    [self getOilRecordRequest];
}

- (void)getOilRecordRequest {
    
    WEAKSELF
    
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *parmas = @{
        @"pageSize":DEFULT_LOAD_NUM,
        @"pageNum":[NSNumber numberWithInteger:_pageNum],
        @"driverCode": [UserDefaultUtil valueForKey:Login_Branch][@"code"],
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], GASSTATIONPAYMENT_LIST) withParameter:parmas withVC:self netName:@"加油记录" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            
            [weakSelf.dataArray addObjectsFromArray:[OilAddRecordModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            weakSelf.foundImageView.hidden = YES;
            [weakSelf.tableView.mj_footer endRefreshing];

        } else {
            
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
            }
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }

        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([DCSpeedy isBlankArr:self.dataArray] == NO) {
        return self.dataArray.count;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OilAddRecordModel *model = self.dataArray[indexPath.row];
    return [DCSpeedy isBlankString:model.gasCode] ? 200 : 222;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OilAddRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self getOilRecordRequest];
}
- (void)loadMoreData{
    _pageNum ++;
    [self getOilRecordRequest];
}

#pragma mark - lazyLoad
- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - 10) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([OilAddRecordCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.tableView.center;
        [self.tableView addSubview:_foundImageView];
    }
    return _foundImageView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
@end
